<?php
	/**
	 * 路由类
	 * Enter description here ...
	 * @author cui
	 *
	 */
	class route extends base
	{
		/**
		 * is self
		 * Enter description here ...
		 * @var unknown_type
		 */
		private static $self;

		/**
		 * 模型名称
		 * Enter description here ...
		 * @var unknown_type
		 */
		private $module_name;

		/**
		 * 控制器名称
		 * Enter description here ...
		 * @var unknown_type
		 */
		private $controller_name;

		/**
		 * 控制器类名
		 * Enter description here ...
		 * @var unknown_type
		 */
		private $class_name;

		/**
		 * 用户访问的域名
		 * Enter description here ...
		 * @var unknown_type
		 */
		private $host_name;

		/**
		 * 构造函数
		 * Enter description here ...
		 */
		private function __construct()
		{

		}

		/**
		 * 单例入口
		 * Enter description here ...
		 */
		public static function get_self()
		{
			if (self::$self)
			{
				return self::$self;
			}

			self::$self = new self();

			return self::$self;
		}

		/**
		 * 路由开始
		 * Enter description here ...
		 */
		public function begin()
		{
			$this->parse_url();
			$this->parse_class_name();
			$this->go_to_controller();
			$this->set_site();
		}

		/**
		 * 去找控制器
		 * Enter description here ...
		 */
		public function go_to_controller()
		{
			controller_helper::go(
				new $this->class_name(),
				array('module_name' => $this->module_name,
					  'controller_name' => $this->controller_name
				)
			);
		}

		/**
		 * URL解析
		 * Enter description here ...
		 */
		private function parse_url()
		{
			$this->host_name = $_SERVER['SERVER_NAME'];

			$uri = $_SERVER['REQUEST_URI'];

		    if ($uri[strlen($uri) - 1] == '/')
		    {
		        $uri = substr($uri, 0, -1);
		    }

		    if (strpos($uri, '/index.php') !== FALSE)
		    {
		        $uri = str_replace("/index.php", "", $uri);
		    }

		    if ($uri == '' || $uri == '/')
		    {
		        $uri = '/index.html';
		    }

		    if (strpos($uri, ".html") !== FALSE)
		    {
	            $uri = substr($uri, 1, -5);
		    }
	        else
	        {
	            $uri = substr($uri, 1);
	        }

	        $uri_info   = explode('-', $uri);

	        if ((count($uri_info) % 2) == 0)
	        {
	            //包含controller 及 action
	            $this->set_module(array_shift($uri_info));
	            $this->set_controller(array_shift($uri_info));
	        }
	        else
	        {
	            //只包含controller
	            $this->set_module(array_shift($uri_info));
	            $this->set_controller('index');
	        }

	        security::get_self()->process_get($uri_info);
		}

		/**
		 * 设置模型名称
		 * Enter description here ...
		 */
		private function set_module($name)
		{
			$this->module_name = $name;
		}

		/**
		 * 分析控制器名称
		 * Enter description here ...
		 */
		private function set_controller($name)
		{
			$this->controller_name = $name;
		}

		/**
		 * 分析控制器类名
		 * Enter description here ...
		 */
		private function parse_class_name()
		{
			$this->class_name = $this->module_name . '_controller_' . $this->controller_name;
		}

		/**
		 * 过滤
		 * Enter description here ...
		 */
		private function filter()
		{

		}

		/**
		 * 抛出异常
		 * Enter description here ...
		 * @param unknown_type $msg
		 */
		private function throw_exception($msg)
		{
			throw new Exception($msg);
		}

		/**
		 * 设置站点
		 * Enter description here ...
		 */
		private function set_site()
		{

		}
	}